package 所有的题类.F.树;

import 所有的题类.ZA.链表.TreeNode;

public class 翻转二叉树 {

    public static void main(String[] args) {
        int[]array={1,2,3,4,5,6,7,8,9,10,11,12,13};
        TreeNode treeNode = arrayToTreeNode(array, 0, array.length - 1);
        翻转二叉树 a =new 翻转二叉树();
        a.invertTree(treeNode);
        System.out.println(treeNode.val);
    }

    public static TreeNode arrayToTreeNode(int[]array,int left,int right){
        TreeNode root=null;
        if(right>=left){
            int mid=(left+right+1)/2;
            root = new TreeNode(array[mid]);
            TreeNode leftNode = arrayToTreeNode(array, left, mid-1);
            TreeNode rightNode = arrayToTreeNode(array, mid + 1, right);
            root.left=leftNode;
            root.right=rightNode;
        }else{
            root=null;
        }
        return root;
    }

    public TreeNode invertTree(TreeNode root) {
        if(root==null){
            return null;
        }
        TreeNode leftNode = invertTree(root.left);
        TreeNode rightNode = invertTree(root.right);
        root.left=rightNode;
        root.right=leftNode;
        return root;
    }
}
